import api from '../../http/modules/api.js';

export default {
    namespaced: true,
    state: {
        pages: 0,
        total: 0,
        classesSearchData: {
            type: 'name',
            value: '',
            pageSize: 4,
            currentPage: 1,
        },
        classesData: [],
    },
    mutations: {
        //获取班级数据成功后，更新班级数据
        setGetClassesData(state, newData) {
            state.pages = newData.pages;
            state.total = newData.total;
            state.classesData = newData.result;
        },
        // 翻页更改数据
        setCurrentPage(state, newData) {
            state.classesSearchData.currentPage = newData;
        },
        // 更改每页条数
        setPageSize(state, newData) {
            state.classesSearchData.pageSize = newData;
        }
    },
    actions: {
        // 获取班级数据
        async getClasses(context) {
            const res = await api.classes.getClassData(context.state.classesSearchData);
            if (res.code) {
                // 获取班级数据成功后，更新班级数据
                context.commit('setGetClassesData', res.data);
            }
        },
        // 根据Id删除一个班级
        async deleteOneClass(context, _id) {
            const res = await api.classes.deleteOneClass({
                _id
            });
            return res;
        },
        // 通过专业Id获取班级数据
        async getClassesBySubjectsId(context, subjectsId) {
            const res = await api.classes.getClassesBySubjectsId({
                subjectsId
            })
            return res;
        },
        // 新增一个班级
        async addOneClass(context,addClassData){
            const res = await api.classes.addOneClass(addClassData)
            return res
        }
    },
};